package com.sh.rabbitmq;

import com.rabbitmq.client.*;

public class Consumer {
    //队列名称
    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) {
        try {
            //创建连接工厂
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("101.33.234.248");
            factory.setUsername("guest");
            factory.setPassword("guest");

            //创建连接
            Connection connection = factory.newConnection();

            //开启信道(传送消息的通道)
            Channel channel = connection.createChannel();

            DeliverCallback deliverCallback = (var1,msg) ->{
                System.out.println(new String(msg.getBody()));
            };

            CancelCallback cancelCallback = var1 ->{
                System.out.println("消费失败");
            };

            channel.basicConsume(QUEUE_NAME,false,deliverCallback,cancelCallback);


        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
